maxbit = 8
class IpAddress():
    def __init__(self):
        self.In = []
        self.Bi = []
        self.nid = ''
        self.hid = ''
        self.No = ''
        
    def GetIp(self):
        """Get IP address"""
        for i in range(1,5):
            b = input("Octet %i: " % i)
            self.In.append(b)
        return self.In
    
    def ToBin(self, bst):
        """Return binary elements of IP address"""
        for i in range(0,len(bst)):
            c = bin(bst[i])[2:]
            while len(c)<maxbit:
                c = '0' + c
            self.Bi.append(c)
        return self.Bi

    def ListToIp(self, lst):
        """Return correct form of IP address"""
        for i in range(0,len(lst)):
            self.No += str(lst[i])+'.'
        """remove last dot"""
        if self.No[len(self.No)-1] == '.':
            self.No = self.No[0:len(self.No)-1]
        return self.No
    
    def NetId(self, lst):
        """Network identificator and host identificator"""
        """lst must be in bin mode"""
        if type(lst[1]) <> str:
            print "IP must be in binary mode!"
            return None
        v = ''
        for i in range(0,len(lst)):
            v += str(lst[i])     
        if v[0] == '0':
            self.nid = v[1:8]
            self.hid = v[8:32]
            print "Class A"
        elif v[0:2] == '10':
            self.nid = v[2:16]
            self.hid = v[16:32]
            print "Class B"
        elif v[0:3] == '110':
            self.nid = v[3:24]
            self.hid = v[24:32]
            print "Class C"
        elif v[0:4] == '1110':
            print "Class D"
            return None
        else:
            print "Class E"
            return None
        print "Network ID =", self.nid, "and Host ID =", self.hid   
    
    def NetMask(self, nm):
        """Define netmask from subnet"""
        """Available only for internet"""
        z = [0,0,0,0]
        v = ''
        q = ''
        oct = []
        lst = ['11111111','11111111','11111111','11111111']
        for i in range(0,len(lst)):
            v += lst[i]
        """n - Number of zeroes from left to right"""  
        n = len(v)-nm
        """Adding 1 in the beginning of mask address"""
        while len(q)<nm:
            q += '1'
        """Adding 0 in the end of mask address"""    
        for j in range(0,n):
            q += '0'
        """Break on octets"""
        for i in range(0,4):
            oct.append(q[8*i:8*(i+1)])
        """Create netmask"""
        for k in range(0,len(oct)):    
            z[k] = int(oct[k][0])*128+int(oct[k][1])*64+int(oct[k][2])*32+int(oct[k][3])*16+int(oct[k][4])*8+int(oct[k][5])*4+int(oct[k][6])*2+int(oct[k][7])*1
        return z
    
    def MaskNet(self, lst):
        """Define subnet from netmask"""
        """Mask must be in bin mode"""
        if type(lst[1]) <> str:
            print "IP must be in binary mode!"
            return None
        v=''
        for i in range(0,len(lst)):
            v += lst[i]    
        return 32-v.count('0') 
 
    
net = IpAddress()

print "Enter mask. Remember it must be an internet mask!\n"
mask = net.GetIp()

bmask = net.ToBin(mask)

print "Subnet is", net.MaskNet(bmask)


